package com.snu.util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class HttpJson {

	private static final String LOG_TAG 	= "HttpJson";	
	
	public String HttpJsonGet(URL url, HashMap<Integer, String> key, HashMap<Integer, String> value){
		//HashMap<Integer, String> mapBuy = new HashMap<Integer, String>(); 		

		String response 			= "";
		HttpURLConnection   conn    = null;		 
		OutputStream          os   	= null;
		InputStream           is   	= null;
		ByteArrayOutputStream baos 	= null;
		 
		try{
			conn = (HttpURLConnection)url.openConnection();	
			conn.setDoInput(true);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
		    String param    = "";		    
		    for(int i=0;i < key.size();i++){
		    	param += key.get(i) + "="+ value.get(i);
		    	if(i != key.size() -1) param += "&";
		    }
		    Log.i(LOG_TAG, "url " + url.toString());
		    Log.i(LOG_TAG, "param " + param);
		    
		    Log.i(LOG_TAG, url.toString() +"?"+ param);	
		    
		    os = conn.getOutputStream();
		    os.write( param.getBytes("UTF-8") );
		    os.flush();
		    os.close();
		
			
			 
			int responseCode = conn.getResponseCode();
			Log.i(LOG_TAG, "responseCode " + responseCode);
			
			if(responseCode == HttpURLConnection.HTTP_OK) {
			 
			    is = conn.getInputStream();
			    baos = new ByteArrayOutputStream();
			    byte[] byteBuffer = new byte[1024];
			    byte[] byteData = null;
			    int nLength = 0;
			    while((nLength = is.read(byteBuffer, 0, byteBuffer.length)) != -1) {
			        baos.write(byteBuffer, 0, nLength);
			    }
			    byteData = baos.toByteArray();
			     
			    response = new String(byteData);
			    
			    
			    Log.i(LOG_TAG, "DATA response = " + response);
			}
		}catch(Exception e){}	
		
				
		return response;
	}
	
	
	/*
	 * 
	 * 
	 * try {				
				// Http Json
				HashMap<Integer, String> key = new HashMap<Integer, String>(); 
				HashMap<Integer, String> value = new HashMap<Integer, String>(); 
				key.put(0, "name");
				value.put(0, "jang tae hoon");
				
				URL url = new URL("http://147.46.170.116:8080/CCRSERVER/json.jsp");
				HttpJson httpJson = new HttpJson();
				String response = httpJson.HttpJsonGet(url,key,value);
				
				
				
			    JSONObject responseJSON = new JSONObject(response);			    
			    String color = (String) responseJSON.get("color");
			    String values = (String) responseJSON.get("value");
			    
			    Log.i(LOG_TAG, "color " + color);
			    Log.i(LOG_TAG, "values " + values);
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
			
	 * 
	 * 
	 * 
	 * 
	 * */
	
}
